package com.example.jdshopdetailtwo.fragment;

import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.jdshopdetailtwo.R;
import com.example.jdshopdetailtwo.adapter.RelatedProductAdapter;
import com.example.jdshopdetailtwo.base.BaseFragment;
import com.example.jdshopdetailtwo.bean.ShopDetailModel;
import com.example.jdshopdetailtwo.util.GlideImageLoader;
import com.example.jdshopdetailtwo.view.AniManager;
import com.example.jdshopdetailtwo.view.CircleImageView;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: njb
 * @date: 2020/5/20 0020 23:11
 * @desc:商品
 */
public class FMCommodity extends BaseFragment {
    @BindView(R.id.shop_banner)
    Banner shopBanner;
    @BindView(R.id.iv_buy_one)
    ImageView ivBuyOne;
    @BindView(R.id.tv_take_name)
    TextView tvTakeName;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_year)
    TextView tvYear;
    @BindView(R.id.tv_mouth_line)
    View tvMouthLine;
    @BindView(R.id.tv_mouth)
    TextView tvMouth;
    @BindView(R.id.tv_day_line)
    View tvDayLine;
    @BindView(R.id.tv_day)
    TextView tvDay;
    @BindView(R.id.rl_take)
    RelativeLayout rlTake;
    @BindView(R.id.shop_name_tv)
    TextView shopNameTv;
    @BindView(R.id.shop_price_tv)
    TextView shopPriceTv;
    @BindView(R.id.shop_vip_price_tv)
    TextView shopVipPriceTv;
    @BindView(R.id.shop_title_tv)
    TextView shopTitleTv;
    @BindView(R.id.sales_volume_tv)
    TextView salesVolumeTv;
    @BindView(R.id.underline_view)
    View underlineView;
    @BindView(R.id.coupon_tv)
    TextView couponTv;
    @BindView(R.id.tv_coupon)
    TextView tvCoupon;
    @BindView(R.id.coupon_content_tv)
    TextView couponContentTv;
    @BindView(R.id.coupon_next_iv)
    ImageView couponNextIv;
    @BindView(R.id.coupon_rl)
    ConstraintLayout couponRl;
    @BindView(R.id.promotions_tv)
    TextView promotionsTv;
    @BindView(R.id.promotions_content_tv)
    TextView promotionsContentTv;
    @BindView(R.id.promotions_next_iv)
    ImageView promotionsNextIv;
    @BindView(R.id.promotions_rl)
    ConstraintLayout promotionsRl;
    @BindView(R.id.preferential_tv)
    TextView preferentialTv;
    @BindView(R.id.integral_content_tv)
    TextView integralContentTv;
    @BindView(R.id.integral_tv)
    TextView integralTv;
    @BindView(R.id.preferential_next_iv)
    ImageView preferentialNextIv;
    @BindView(R.id.integral_rl)
    ConstraintLayout integralRl;
    @BindView(R.id.coll_toolbar_layout)
    CollapsingToolbarLayout collToolbarLayout;
    @BindView(R.id.al_shop_detail)
    AppBarLayout alShopDetail;
    @BindView(R.id.tv_params)
    TextView tvParams;
    @BindView(R.id.tv_effect)
    TextView tvEffect;
    @BindView(R.id.iv_next)
    ImageView ivNext;
    @BindView(R.id.cl_params)
    ConstraintLayout clParams;
    @BindView(R.id.tv_evaluation)
    TextView tvEvaluation;
    @BindView(R.id.tv_more_evaluation)
    TextView tvMoreEvaluation;
    @BindView(R.id.cl_more_evaluation)
    ConstraintLayout clMoreEvaluation;
    @BindView(R.id.civ_user)
    CircleImageView civUser;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_evaluation_content)
    TextView tvEvaluationContent;
    @BindView(R.id.rb_related_products)
    RadioButton rbRelatedProducts;
    @BindView(R.id.rb_watch)
    RadioButton rbWatch;
    @BindView(R.id.rv_product)
    RecyclerView rvProduct;
    @BindView(R.id.tv_pull)
    TextView tvPull;
    @BindView(R.id.nsv_good)
    NestedScrollView nsvGood;
    @BindView(R.id.cl_good)
    CoordinatorLayout clGood;
    @BindView(R.id.cart_iv)
    ImageView cartIv;
    @BindView(R.id.tv_cart_count)
    TextView tvCartCount;
    @BindView(R.id.service_iv)
    ImageView serviceIv;
    @BindView(R.id.collection_iv)
    ImageView collectionIv;
    @BindView(R.id.ll_add)
    LinearLayout llAdd;
    @BindView(R.id.add_cart_btn)
    Button addCartBtn;
    @BindView(R.id.buy_now_btn)
    Button buyNowBtn;
    @BindView(R.id.ll_cart)
    LinearLayout llCart;
    @BindView(R.id.content_cl)
    ConstraintLayout contentCl;
    private RelatedProductAdapter shopAdapter;
    private ConstraintLayout llHead;
    private TabLayout shopTabLayout;
    private ViewPager viewPager;
    private int mAlpha;
    private String brand_id;

    @Override
    protected void loadData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fm_commondity;
    }

    @Override
    protected void initView() {
        //滑动
        initScrollView();
        initAdapter();
        initBanner();
    }

    public void initScrollView() {
        alShopDetail.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            public boolean isBottom;

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (appBarLayout.getTotalScrollRange() == -verticalOffset) {
                    if (isBottom) {
//                        viewPager.setCurrentItem(1);
                    } else {
                        isBottom = true;
                    }
                } else {
                    isBottom = false;
                }
                int minHeight = 50;
                if (shopBanner != null && shopBanner.getMeasuredHeight() != 0) {
                    int maxHeight = shopBanner.getMeasuredHeight() / 2;
                    if (maxHeight == 0) {
                        maxHeight = 255;
                    }
                    if (Math.abs(verticalOffset) <= minHeight) {
                        mAlpha = 0;
                    } else if (Math.abs(verticalOffset) > maxHeight) {
                        mAlpha = 255;
                    } else {
                        mAlpha = (Math.abs(verticalOffset) - minHeight) * 200 / (maxHeight - minHeight);
                    }
                    if (mAlpha <= 0) {
                        llHead.setBackgroundColor(Color.argb(0, 255, 255, 255));
                    } else if (mAlpha >= 255) {
                        llHead.setBackgroundColor(Color.argb(mAlpha, 255, 255, 255));
                        shopTabLayout.setVisibility(View.VISIBLE);
                    } else {
                        llHead.setBackgroundColor(Color.argb(mAlpha, 255, 255, 255));
                        shopTabLayout.setVisibility(View.GONE);
                    }
                }
            }
        });
    }
    private void initAdapter() {
        shopAdapter = new RelatedProductAdapter(null);
        rvProduct.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        shopAdapter.bindToRecyclerView(rvProduct);
        shopAdapter.setEmptyView(R.layout.empty_goods_list);
        rvProduct.setAdapter(shopAdapter);
    }

    private void initBanner() {
        List<String> list = new ArrayList<>();
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg");

        //设置banner样式
        shopBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        shopBanner.setImageLoader(new GlideImageLoader());
        //设置banner动画效果
        shopBanner.setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        shopBanner.isAutoPlay(true);
        //设置轮播时间
        shopBanner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        shopBanner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        shopBanner.setImages(list);
        shopBanner.start();
    }


    public void setHead(ConstraintLayout llHead, TabLayout shopTabLayout, ViewPager viewPager) {
        this.llHead = llHead;
        this.shopTabLayout = shopTabLayout;
        this.viewPager = viewPager;
    }

    @Override
    protected void addListener() {

    }

    @OnClick({R.id.coupon_rl, R.id.promotions_rl, R.id.cl_params, R.id.collection_iv, R.id.tv_pull, R.id.cart_iv, R.id.service_iv, R.id.add_cart_btn, R.id.buy_now_btn, R.id.rb_related_products, R.id.rb_watch, R.id.cl_more_evaluation,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.coupon_rl://领优惠券
                //showCouponDialog();
                break;
            case R.id.promotions_rl://促销活动
                //showPromotionsDialog();
                break;
            case R.id.cl_params://产品参数

                break;
            case R.id.add_cart_btn://加入购物车
                break;
            case R.id.buy_now_btn://立即购买
                break;
            case R.id.cl_more_evaluation://更多评价
                viewPager.setCurrentItem(1);
                break;
            case R.id.tv_pull://上拉查看商品详情
                viewPager.setCurrentItem(2);
                break;
            case R.id.rb_related_products:
                rbWatch.setChecked(false);
                brand_id = "14";
                rbRelatedProducts.setChecked(true);
                break;
            case R.id.rb_watch:
                rbRelatedProducts.setChecked(false);
                brand_id = "15";
                rbWatch.setChecked(true);
                break;
            case R.id.cart_iv:
                break;
            case R.id.service_iv://客服
                break;
            case R.id.collection_iv://收藏商品
                break;
        }
    }
}
